#include "gumballmachine.h"

#include "hasquarterstate.h"
#include "noquarter.h"
#include "sellingstate.h"
#include "soldoutstate.h"

#include <qDebug>

GumballMachine::GumballMachine(int count)
    : m_count(count)
    , m_noQuarter(new NoQuarterState(this))
    , m_hasQuarter(new HasQuarterState(this))
    , m_selling(new SellingState(this))
    , m_soldOut(new SoldOutState(this))
{
    if (m_count > 0)
    {
        m_curState = m_noQuarter;
    }
}

void GumballMachine::insertQuarter()
{
    if (m_curState)
    {
        m_curState->insertQuarter();
    }
}

void GumballMachine::ejectQuarter()
{
    if (m_curState)
    {
        m_curState->ejectQuarter();
    }
}

void GumballMachine::turnCrank()
{
    if (m_curState)
    {
        m_curState->turnCrank();
    }
}

void GumballMachine::dispense()
{
    if (m_curState)
    {
        m_curState->dispense();
    }
}

void GumballMachine::setState(State *state)
{
    m_curState = state;
    if (m_curState == m_selling)
    {
        m_curState->dispense();
    }
}

State *GumballMachine::getNoQuarterState()
{
    return m_noQuarter;
}

State *GumballMachine::getHasQuarterState()
{
    return m_hasQuarter;
}

State *GumballMachine::getSellingState()
{
    return m_selling;
}

State *GumballMachine::getSoldOutState()
{
    return m_soldOut;
}

void GumballMachine::releaseGumBall()
{
    m_count--;
    qDebug() << "congratulations, you got a gum ball!";
}

int GumballMachine::getCount()
{
    return m_count;
}
